﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

#include "widget.h"
#include "ui_widget.h"
#include "scanrulesavedialog.h"
#include <QMessageBox>
#include <delegatecenter.h>
#include <QDebug>
#include <QMenu>
#include <QFileDialog>
#include <qglobalconfig.h>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QGlobalConfig::getInstance().init(QCoreApplication::applicationDirPath() + "/" + QCoreApplication::applicationName() + ".ini");
    ui->scanDir->setText(QGlobalConfig::getInstance().read("param","scanDir","").toString());

    disposeStatus = false;
    disposeThread = new DisposeThread();
    connect(disposeThread,&DisposeThread::signal_workStatus,this,&Widget::slot_workStatus);
    connect(disposeThread,&DisposeThread::signal_workMsg,this,&Widget::slot_workMsg);

    tableModel = new QStandardItemModel();
    ui->tableView->setModel(tableModel);

    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//单行选中
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置单元格不可编辑

    // 设置表头数据
    QStringList headers;
    headers << "id" << "过滤文件名" << "处理模式" << "升级文件路径";
    tableModel->setHorizontalHeaderLabels(headers);

    QStringList enumList1{ QString("删除"), QString("升级") };
    EnumDelegate * enumDelegate1 = new EnumDelegate(enumList1, this);

    ui->tableView->setColumnHidden(0,true);

    ui->tableView->setItemDelegateForColumn(2, enumDelegate1);

    selectAll();
}

Widget::~Widget()
{
    delete disposeThread;
    QGlobalConfig::getInstance().deleteInstance();
    delete ui;
}

void Widget::selectAll()
{
    tableModel->removeRows(0,tableModel->rowCount());
    QList<ScanRule> list;
    if(objectService.getList(list)){
        for (ScanRule &item:list) {
            qDebug() << "插入新行";
            // 插入新行
            QList<QStandardItem*> rowItems;
            rowItems << new QStandardItem(QString::number(item.getId()))
                     << new QStandardItem(item.getFilterName())
                     << new QStandardItem(QString::number(item.getDisposeMode()))
                     << new QStandardItem(item.getReplacePath());

            rowItems[0]->setData(item.getId());
            rowItems[1]->setData(item.getFilterName());
            rowItems[2]->setData(item.getDisposeMode());
            rowItems[3]->setData(item.getReplacePath());

            tableModel->appendRow(rowItems);
        }
//        ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
//        ui->tableView->resizeRowToContents(0);
    }else{
        QMessageBox::critical(this,"警告","数据库连接异常！");
    }
}

void Widget::addScanRule()
{
    ScanRule objectInfo;
    ScanRuleSaveDialog *saveDialog = new ScanRuleSaveDialog(&objectInfo,this);
    saveDialog->setWindowIcon(QIcon(":/res/img/ico/add.ico"));
    if(saveDialog->exec() == QDialog::Accepted){
        objectService.saveOne(objectInfo);
        selectAll();
    }
}

void Widget::upScanRule()
{
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();

    ScanRule objectInfo;
    objectInfo.setId(ui->tableView->model()->index(iSel,0).data().toInt());
    objectInfo.setFilterName(ui->tableView->model()->index(iSel,1).data().toString());
    objectInfo.setDisposeMode(ui->tableView->model()->index(iSel,2).data().toInt());
    objectInfo.setReplacePath(ui->tableView->model()->index(iSel,3).data().toString());
    ScanRuleSaveDialog *saveDialog = new ScanRuleSaveDialog(&objectInfo,this);
    saveDialog->setWindowIcon(QIcon(":/res/img/ico/edit.ico"));
    if(saveDialog->exec() == QDialog::Accepted){
        objectService.saveOne(objectInfo);
        selectAll();
    }
}

void Widget::delScanRule()
{
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();

    ScanRule ScanRuleInfo;
    ScanRuleInfo.setId(ui->tableView->model()->index(iSel,0).data().toInt());
    objectService.deleteOne(ScanRuleInfo);
    selectAll();
}

void Widget::slot_workStatus(bool isRun)
{
    disposeStatus = isRun;
    ui->switchBtn->setText(disposeStatus ? "停止" : "启动");
    ui->groupBox->setEnabled(!disposeStatus);
}

void Widget::slot_workMsg(QString msg)
{
    ui->workStatus->setText(msg);
}


void Widget::on_addRule_clicked()
{
    addScanRule();
}

void Widget::on_tableView_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu(this);
    QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    int iSel = index.row();
    menu.addAction(QIcon(":/res/img/ico/add.ico"),QString("添加"),this,&Widget::addScanRule);
    if(iSel > -1){
        menu.addAction(QIcon(":/res/img/ico/edit.ico"),QString("修改"),this,&Widget::upScanRule);
        menu.addAction(QIcon(":/res/img/ico/delete.ico"),QString("删除"),this,&Widget::delScanRule);
    }
    menu.exec(QCursor::pos());
}

void Widget::on_switchBtn_clicked()
{
    if(disposeStatus){
        disposeThread->stop();
    }else{
        disposeThread->start();
    }
}

void Widget::on_selScanDirBth_clicked()
{
    QString openDir=ui->scanDir->text();

    QString dir = QFileDialog::getExistingDirectory(this,QString("选择待扫描路径"),openDir);
    if(!dir.isEmpty()){
        ui->scanDir->setText(dir);
        QGlobalConfig::getInstance().write("param","scanDir",dir);
    }
}

void Widget::on_scanDir_editingFinished()
{
    QGlobalConfig::getInstance().write("param","scanDir",ui->scanDir->text());
}
